#include "definition.h"
#include "TxtDoc.h"
#include "../UI/MagicDocView.h"

IMPLEMENT_CLASS(wxTextDocument, Document)

bool wxTextDocument::DoSaveDocument(const wxString& filename)
{
    if( !Document::DoSaveDocument(filename) )
        throw std::logic_error("Error while saving text document");

    return GetTextCtrl()->SaveFile(filename);
}

bool wxTextDocument::DoOpenDocument(const wxString& filename)
{
    Document::DoOpenDocument(filename);
    return GetTextCtrl()->LoadFile(filename);
}

bool wxTextDocument::IsModified() const
{
    wxTextCtrl* wnd = GetTextCtrl();
    return wxDocument::IsModified() || (wnd && wnd->IsModified());
}

void wxTextDocument::Modify(bool modified)
{
    wxDocument::Modify(modified);

    wxTextCtrl* wnd = GetTextCtrl();
    if (wnd && !modified)
    {
        wnd->DiscardEdits();
    }
}

IMPLEMENT_DYNAMIC_CLASS(TextEditDocument, Document)

wxTextCtrl* TextEditDocument::GetTextCtrl() const
{
    wxView* view = GetFirstView();
    return view ? wxStaticCast(view, TextEditView)->GetText() : NULL;
}

void TextEditDocument::SetContent(STRING content)
{
    (*GetTextCtrl()) << content ;
}